package com.solcoo.util;

import org.apache.log4j.Logger;

import cn.jpush.api.ErrorCodeEnum;
import cn.jpush.api.JPushClient;
import cn.jpush.api.MessageResult;

public class JpushUtil implements Runnable {
	
	private static Logger logger = Logger.getLogger(JpushUtil.class);
	
	private static final String USERNAME = "15381031851"; // username
	private static final String PASSWORD = "65624982"; // password
	private static final String APPKEY = "862934194a5d043d8dba6c2e"; // appkey
	
	private String alias;
	private String msg;
	
	
	public JpushUtil(String alias, String msg) {
		super();
		this.alias = alias;
		this.msg = msg;
	}

	@Override
	public void run() {
		JPushClient jpush = new JPushClient(USERNAME, PASSWORD, APPKEY);
		
		MessageResult msgResult = jpush.sendNotificationWithAlias(2013, alias, "title", msg);
		
		if (null != msgResult) {
			if (msgResult.getErrcode() == ErrorCodeEnum.NOERROR.value()) {
				logger.info("发送成功， sendNo=" + msgResult.getSendno());
			} else {
				logger.error("发送失败， 错误代码=" + msgResult.getErrcode() + ", 错误消息=" + msgResult.getErrmsg());
			}
		} else {
			logger.error("无法获取数据，检查网络是否超时");
		}
	}
}